<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="会员ID" prop="memberId">
      <el-input v-model="dataForm.memberId" placeholder="会员ID"></el-input>
    </el-form-item>
    <el-form-item label="设备编码" prop="deviceCode">
      <el-input v-model="dataForm.deviceCode" placeholder="设备编码"></el-input>
    </el-form-item>
    <el-form-item label="设备内网ip地址" prop="deviceIp">
      <el-input v-model="dataForm.deviceIp" placeholder="设备内网ip地址"></el-input>
    </el-form-item>
    <el-form-item label="状态：1正常，2锁定，3注销" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态：1正常，2锁定，3注销"></el-input>
    </el-form-item>
    <el-form-item label="" prop="creator">
      <el-input v-model="dataForm.creator" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updater">
      <el-input v-model="dataForm.updater" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="在线状态 1:在线；2：离线" prop="onlineStatus">
      <el-input v-model="dataForm.onlineStatus" placeholder="在线状态 1:在线；2：离线"></el-input>
    </el-form-item>
    <el-form-item label="失效日期" prop="expirationTime">
      <el-input v-model="dataForm.expirationTime" placeholder="失效日期"></el-input>
    </el-form-item>
    <el-form-item label="版本号" prop="versionNo">
      <el-input v-model="dataForm.versionNo" placeholder="版本号"></el-input>
    </el-form-item>
    <el-form-item label="型号" prop="deviceModel">
      <el-input v-model="dataForm.deviceModel" placeholder="型号"></el-input>
    </el-form-item>
    <el-form-item label="设备名称" prop="deviceName">
      <el-input v-model="dataForm.deviceName" placeholder="设备名称"></el-input>
    </el-form-item>
    <el-form-item label="设备安装地址： 省，市，区及详细地址以空格隔开" prop="deviceAddr">
      <el-input v-model="dataForm.deviceAddr" placeholder="设备安装地址： 省，市，区及详细地址以空格隔开"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          memberId: '',
          deviceCode: '',
          deviceIp: '',
          status: '',
          creator: '',
          createTime: '',
          updater: '',
          updateTime: '',
          onlineStatus: '',
          expirationTime: '',
          versionNo: '',
          deviceModel: '',
          deviceName: '',
          deviceAddr: ''
        },
        dataRule: {
          memberId: [
            { required: true, message: '会员ID不能为空', trigger: 'blur' }
          ],
          deviceCode: [
            { required: true, message: '设备编码不能为空', trigger: 'blur' }
          ],
          deviceIp: [
            { required: true, message: '设备内网ip地址不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态：1正常，2锁定，3注销不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updater: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          onlineStatus: [
            { required: true, message: '在线状态 1:在线；2：离线不能为空', trigger: 'blur' }
          ],
          expirationTime: [
            { required: true, message: '失效日期不能为空', trigger: 'blur' }
          ],
          versionNo: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          deviceModel: [
            { required: true, message: '型号不能为空', trigger: 'blur' }
          ],
          deviceName: [
            { required: true, message: '设备名称不能为空', trigger: 'blur' }
          ],
          deviceAddr: [
            { required: true, message: '设备安装地址： 省，市，区及详细地址以空格隔开不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/disk/membernetdisk/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberId = data.memberNetdisk.memberId
                this.dataForm.deviceCode = data.memberNetdisk.deviceCode
                this.dataForm.deviceIp = data.memberNetdisk.deviceIp
                this.dataForm.status = data.memberNetdisk.status
                this.dataForm.creator = data.memberNetdisk.creator
                this.dataForm.createTime = data.memberNetdisk.createTime
                this.dataForm.updater = data.memberNetdisk.updater
                this.dataForm.updateTime = data.memberNetdisk.updateTime
                this.dataForm.onlineStatus = data.memberNetdisk.onlineStatus
                this.dataForm.expirationTime = data.memberNetdisk.expirationTime
                this.dataForm.versionNo = data.memberNetdisk.versionNo
                this.dataForm.deviceModel = data.memberNetdisk.deviceModel
                this.dataForm.deviceName = data.memberNetdisk.deviceName
                this.dataForm.deviceAddr = data.memberNetdisk.deviceAddr
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/disk/membernetdisk/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'memberId': this.dataForm.memberId,
                'deviceCode': this.dataForm.deviceCode,
                'deviceIp': this.dataForm.deviceIp,
                'status': this.dataForm.status,
                'creator': this.dataForm.creator,
                'createTime': this.dataForm.createTime,
                'updater': this.dataForm.updater,
                'updateTime': this.dataForm.updateTime,
                'onlineStatus': this.dataForm.onlineStatus,
                'expirationTime': this.dataForm.expirationTime,
                'versionNo': this.dataForm.versionNo,
                'deviceModel': this.dataForm.deviceModel,
                'deviceName': this.dataForm.deviceName,
                'deviceAddr': this.dataForm.deviceAddr
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
